import pytest
import requests
import random


class TestSpeechcraft():

    t_url = "http://60.205.190.66:8899"
    num = random.randint(1, 1000)
    id_list = []
    @pytest.fixture(scope="function", autouse=True)
    def login(self):
        resw = requests.post(self.t_url + "/adminapi/login", json={"account": "admin", "pwd": "920509", "imgcode": "x"})
        self.token = resw.json()['data']['token']
        yield
        requests.get(self.t_url + '/adminapi/setting/admin/logout')

    @pytest.mark.flaky(reruns=3, reruns_delay=1)
    def test_a(self):
        res1 = requests.post(self.t_url + "/adminapi/app/wechat/speechcraft",
                             json={"cate_id": 0, "title": "秒速五厘米",
                                   "message": '樱花落下的速度，是每秒五厘米。那我用什么样的速度才能与你相遇' + str(self.num), "sort": 0},
                             headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        print(res1.json())

    def test_b(self):
        res2 = requests.get(self.t_url + "/adminapi/app/wechat/speechcraft?page=1&limit=100&cate_id=",
                            headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        # print(res2.json()['data']['list'])
        for i in res2.json()['data']['list']:
            if i["message"] == '樱花落下的速度，是每秒五厘米。那我用什么样的速度才能与你相遇' + str(self.num):
                self.id_list.append(i['id'])

    def test_c(self):
        res3 = requests.delete(self.t_url + "/adminapi/app/wechat/speechcraft/" + str(self.id_list[0]),
                               headers={"Authori-Zation": 'Bearer ' + str(self.token)})
        print(res3.json())

if __name__ == '__main__':
    pytest.main()